package GUI;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;

import Control.Ctr;
import Model.Booking;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Main {

	private JFrame frame;
	private ArrayList<Booking> booking;
	private Ctr ctr;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		ctr = new Ctr();
		booking = new ArrayList<Booking>();
		
		
		
		frame = new JFrame();
		frame.setBounds(100, 100, 298, 150);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnClient = new JButton("Client");
		btnClient.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.Client.main(null);
			}
		});
		btnClient.setBounds(10, 11, 125, 23);
		frame.getContentPane().add(btnClient);
		
		JButton btnBooking = new JButton("Booking");
		btnBooking.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.Booking.main(null);	
			}
		});
		btnBooking.setBounds(155, 11, 125, 23);
		frame.getContentPane().add(btnBooking);
		
		JButton btnAdministrator = new JButton("Administrator");
		btnAdministrator.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.Admin.main(null);
				
			}
		});
		btnAdministrator.setBounds(84, 45, 119, 23);
		frame.getContentPane().add(btnAdministrator);
		
		final JLabel lblUpdate = new JLabel("Update");
		lblUpdate.setHorizontalAlignment(SwingConstants.CENTER);
		lblUpdate.setBounds(10, 79, 270, 23);
		frame.getContentPane().add(lblUpdate);
		
		
		
		
         // Create a new Thread to do the counting
         Thread t = new Thread() {
            @Override
            public void run() {  // override the run() for the running behaviors
            	int t=0;
            	for (int i = 0; i > -1; ++i)//Never Stop
            	{	t++;
            		if(t==120)
            		{t=0;
            		booking.clear();
            		}
                  	DateFormat dateF = new SimpleDateFormat("yyyy-MM-dd");
                  	DateFormat timeF = new SimpleDateFormat("HH:mm");
  		   			Date date= new Date();
                  	String d = dateF.format(date);
  		   			for(Model.Booking e :ctr.findAllBookingByDate(d))
  		   			{
  		   			if(booking.contains(e)==false)
  		   			 {Date time = new Date();
  		   			 String y = timeF.format(time);
  		   			 y= y.substring(0,y.length() -2);
  		   				if(e.getsStart().contains(y) )
  		   				
  		   				{	
  		   					int cID = e.getcID();
  		   					
  		   					boolean b=ctr.updateClient(cID, 5, "", 1);//Subtracts 1 session from client as it is considered that it started
  		   					booking.add(e);
  		   					if(b)
  		   					lblUpdate.setText("Client "+cID+" started his session!");
  		   					else
  		   						lblUpdate.setText("Next Update:"+y+1);
  		   					}}
  		   			}
                  // Suspends this thread via sleep() and yield control to other threads.
                  // Also provide the necessary delay.
                  try {
                     sleep(60000);
                     // milliseconds 60 sec
                  } catch (InterruptedException ex) {}
               }
            }
         };
         t.start();  // call back run()
		//time.main(null);
	}
}
